using System;
using System.Collections.Generic;
using System.Text;

namespace SymAutomate.LetterFile
{
    /// <summary>
    /// Handles Letter File functions.
    /// Error Numbers 510-520; Reference file ErrorNumbers.txt for more information.
    /// 510 Error: Letter file '%letter file name%' does not exist.
    /// </summary>
    class Driver : DriverBase, DriverInterface
    {
        private Parameters __Parameters;

        public Driver()
            : base()
        {
        }

        public Driver(ConsoleArguments _ConsoleArguments, HostTelnetConnection.Driver _HostTelnetConnectionDriver)
            : base(_HostTelnetConnectionDriver)
        {
            __Parameters = new Parameters(_ConsoleArguments, base.ConsoleHelpList);
        }

        /// <summary>
        /// Defines the command to function mapping;
        /// First parameter is what to expect as "command" from commmand line;
        /// Second parameter is the function name (case must match);
        /// </summary>
        public void PopulateCommandToFunctionMapping()
        {
            CommandToFunctionMapping.Add("DeleteLetterFile".ToLower(), "DeleteLetterFile");
        }

        /// <summary>
        /// Defines the help text for this driver
        /// </summary>
        public void PopulateConsoleHelp()
        {
            ConsoleHelp _ConsoleHelp = new ConsoleHelp("DeleteLetterFile");
            _ConsoleHelp.Parameters.Add("FileName");
            ConsoleHelpList.Add(_ConsoleHelp);
        }

        /// <summary>
        /// Implements Letter File Control -> Delete a Letter File;
        /// Deletes a letter file from the Symitar system.
        /// </summary>
        public void DeleteLetterFile()
        {
            string s = "";

            try
            {
                HostTelnetConnectionDriver.Login();

                HostTelnetConnectionDriver.TelnetSend("8" + GlobalParameters.TelnetSendEndOfCommand); //Management Menu
                s = HostTelnetConnectionDriver.TelnetWaitFor("Menu Selection");
                HostTelnetConnectionDriver.LogIncommingStream(s);

                HostTelnetConnectionDriver.TelnetSend("4" + GlobalParameters.TelnetSendEndOfCommand); //Letter File Control
                s = HostTelnetConnectionDriver.TelnetWaitFor("Selection");
                HostTelnetConnectionDriver.LogIncommingStream(s);

                HostTelnetConnectionDriver.TelnetSend("4" + GlobalParameters.TelnetSendEndOfCommand); //Delete a Letter File
                s = HostTelnetConnectionDriver.TelnetWaitFor("Letter File Name");
                HostTelnetConnectionDriver.LogIncommingStream(s);

                HostTelnetConnectionDriver.TelnetSend(__Parameters.FileName + GlobalParameters.TelnetSendEndOfCommand); //Letter File Name
                s = HostTelnetConnectionDriver.TelnetWaitFor(new string[] { "Okay?", "No such file or directory" });
                s = HostTelnetConnectionDriver.LogIncommingStream(s);

                if (s.Contains("No such file or directory")) throw new ReturnException("510 Error: Letter file '" + __Parameters.FileName + "' does not exist.");

                HostTelnetConnectionDriver.TelnetSend("Y" + GlobalParameters.TelnetSendEndOfCommand); //'Y' confirmation
                s = HostTelnetConnectionDriver.TelnetWaitFor("Letter File Name");
                HostTelnetConnectionDriver.LogIncommingStream(s);
            }
            catch (Exception e)
            {
                if (e is Dart.Telnet.DataException)
                    s = HostTelnetConnectionDriver.LogIncommingStream(((Dart.Telnet.DataException)e).DataRead.ToString());
                else
                    s = HostTelnetConnectionDriver.LogIncommingStream(string.Empty);
                HostTelnetConnectionDriver.HandleException(e, s);
            }
            finally
            {
                HostTelnetConnectionDriver.TelnetCloseConnection();
            }
        }
    }
}
